package cn.jimmie.learn.viewArt.refreshview.core;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import cn.jimmie.learn.viewArt.R;

/**
 * function :
 *
 * <p> Create Time:  2019年04月11日 22:36 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class CommonDecorHolder extends DecorHolder {
    private static final String PULL_DOWN_TEXT = "下拉刷新";
    private static final String RELEASE_TEXT = "释放更新";
    private static final String REFRESHING_TEXT = "加载中...";

    private static final String NO_MORE = "已无更多...";
    private static final String LOAD_FAIL = "加载失败...";

    // 头部相关
    private TextView mHeaderTextView;
    private ImageView mHeaderArrowView;
    private ImageView mHeaderLoadView;
    private AnimationDrawable mHeaderLoadAnimateDw;
    private RotateAnimation mHeaderUpAnim;

    // 底部相关
    private AnimationDrawable mFooterLoadAnimatorDw;
    private TextView mFooterTextView;
    private ImageView mFooterImageView;

    public CommonDecorHolder(Context context) {
        super(context);
        initAnimation();
    }

    private void initAnimation() {
        mHeaderUpAnim = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mHeaderUpAnim.setDuration(150);
        mHeaderUpAnim.setFillAfter(true);
    }


    @Override
    View createHeaderView() {
        @SuppressLint("InflateParams")
        View header = LayoutInflater.from(getContext()).inflate(R.layout.header_view, null);
        mHeaderTextView = header.findViewById(R.id.normal_refresh_header_status);
        mHeaderArrowView = header.findViewById(R.id.normal_refresh_header_arrow);
        mHeaderLoadView = header.findViewById(R.id.normal_refresh_header_loading);
        mHeaderLoadAnimateDw = (AnimationDrawable) mHeaderLoadView.getDrawable();
        mHeaderTextView.setText(PULL_DOWN_TEXT);

        return header;
    }

    @Override
    View createFooterView() {
        @SuppressLint("InflateParams")
        View footer = LayoutInflater.from(getContext()).inflate(R.layout.footer_view, null);
        mFooterImageView = footer.findViewById(R.id.refresh_footer_load_view);
        mFooterTextView = footer.findViewById(R.id.refresh_footer_text_view);
        mFooterLoadAnimatorDw = (AnimationDrawable) mFooterImageView.getDrawable();
//        footer.setEnabled(false);
        return footer;
    }

    @Override
    public void onHeaderRefreshPullDown() {
        mHeaderTextView.setText(PULL_DOWN_TEXT);
        mHeaderLoadView.setVisibility(View.INVISIBLE);
        mHeaderLoadAnimateDw.stop();
        mHeaderArrowView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onHeaderRefreshFullItem() {
        mHeaderTextView.setText(RELEASE_TEXT);
        mHeaderLoadView.setVisibility(View.INVISIBLE);
        mHeaderLoadAnimateDw.stop();
        mHeaderArrowView.setVisibility(View.VISIBLE);
        mHeaderArrowView.startAnimation(mHeaderUpAnim);
    }

    @Override
    public void onHeaderRefreshRelease(boolean overItem) {
    }

    @Override
    public void onHeaderRefreshing() {
        mHeaderTextView.setText(REFRESHING_TEXT);
        // 必须把动画清空才能隐藏成功
        mHeaderArrowView.clearAnimation();
        mHeaderArrowView.setVisibility(View.INVISIBLE);
        mHeaderLoadView.setVisibility(View.VISIBLE);
        mHeaderLoadAnimateDw.start();
    }

    @Override
    public void onHeaderRefreshFinish() {
        mHeaderTextView.setText(PULL_DOWN_TEXT);
        mHeaderLoadView.setVisibility(View.INVISIBLE);
        mHeaderLoadAnimateDw.stop();
        mHeaderArrowView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onFooterLoading() {
        mFooterTextView.setText(REFRESHING_TEXT);
        mFooterImageView.setVisibility(View.VISIBLE);
        mFooterTextView.setVisibility(View.VISIBLE);
        mFooterLoadAnimatorDw.start();
    }

    @Override
    public void onFooterLoadFinish(RefreshState.ResultState state) {
        mFooterLoadAnimatorDw.stop();
        mFooterImageView.setVisibility(View.GONE);

        if (state == RefreshState.ResultState.FAILURE) {
            mFooterTextView.setText(LOAD_FAIL);
        } else if (state == RefreshState.ResultState.END) {
            mFooterTextView.setText(NO_MORE);
        } else getFootView().setVisibility(View.GONE);
    }
}
